package com.roc.web.action.table;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.YSBrealNameRejectBean;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.CompTransCount;
import com.roc.pojo.Operators;
import com.roc.service.IPagenitionService;
import com.roc.util.Constance;
import com.roc.util.constant.AuthReason;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
/**
 * 云商宝实名驳回统计
 * @author jdd
 *
 */
@Action
@Component
@Scope("prototype")
@Namespace("/YSBrealName")
public class YSBrealNameRejectActiom  extends PaginationBaseAction{

	private static final long serialVersionUID = 1L;
	private Logger logger=LogManager.getLogger(YSBrealNameRejectActiom.class);
	private YSBrealNameRejectSearchCondtion  searchCondition;
	

	@Autowired
	private IPagenitionService  pagenitionService;
	
	@Action(value="init",results={ @Result(name="success",location="/pages/table/ysbRealNameRejectJsp.jsp")})
	public String init(){
		Operators op = (Operators) getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if (op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode())
				|| op.getRoleType().equals(Role.SYSTEM.getCode())) {
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;
	}
	/**
	 * 云商宝实名驳回统计查询
	 * @return
	 */
	@Action(value="queryYSBrealNameReject",results={@Result(name="success",location="/pages/table/ysbRealNameRejectJsp.jsp")})
	public String queryCompTransList(){
		
		
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.OPERATORS_COMM.getCode()) || op.getRoleType().equals(Role.OPERATORS_LEAD.getCode())|| op.getRoleType().equals(Role.SYSTEM.getCode())||op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode()))
		{
		if(searchCondition==null){
			searchCondition=(YSBrealNameRejectSearchCondtion)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new YSBrealNameRejectSearchCondtion();
			}
		}
		try {
			int rows = pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			}
				List<YSBrealNameRejectBean> cclist=new ArrayList<YSBrealNameRejectBean>();
				//查询
				dealWith(cclist,searchRsList);
			
			getRequest().setAttribute("cclist", cclist);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			addActionMessage("当前条件下统计到【"+rows+"】条记录");
		} catch (Exception e) {
			logger.info("查询商终明细数据异常："+e);
			addActionError(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}
		addActionError("无权限访问!!!");
		return ERROR;	
	}
	
	
	
	
	
	private List<YSBrealNameRejectBean> dealWith(List<YSBrealNameRejectBean> ccList, List<Object> searchRsList) {
		// TODO Auto-generated method stub
		if(searchRsList!=null&&searchRsList.size()>0)
		{
			Iterator<?> iterator=searchRsList.iterator();
			while(iterator.hasNext())
			{
				Object[] obj=(Object[]) iterator.next();
				YSBrealNameRejectBean com=new YSBrealNameRejectBean();
//				String resultCode = AuthReason.valueOfCode(Constance.Null2EmptyTrim(obj[0]));
				com.setResultDetail(Constance.Null2EmptyTrim(obj[0]));
			    com.setCount(Constance.Null2EmptyTrim(obj[1]));
			    NumberFormat nf = NumberFormat.getPercentInstance();
			    nf.setMaximumFractionDigits(2);
			    com.setProportion(Constance.Null2EmptyTrim(nf.format(obj[2])));
			    com.setSumDetail(Constance.Null2EmptyTrim(obj[3]));
				ccList.add(com);
			}
		}
		return ccList;
	}
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
		
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_YS_BREAL_NAME_REJECT, searchCondition);
		
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (YSBrealNameRejectSearchCondtion)getSession().getAttribute(SEARCH_SESSION_YS_BREAL_NAME_REJECT);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}
	
	public YSBrealNameRejectSearchCondtion getSearchCondition() {
		return searchCondition;
	}
	
	public void setSearchCondition(YSBrealNameRejectSearchCondtion searchCondition) {
		this.searchCondition = searchCondition;
	}
}
